/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.MailSender;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.CalendarMailSender;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.RecurId;
/*     */ import com.zimbra.cs.mailbox.calendar.ZAttendee;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CancelCalendarItem
/*     */   extends CalendarRequest
/*     */ {
/*  54 */   private static final String[] TARGET_ITEM_PATH = { "id" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  58 */     return TARGET_ITEM_PATH;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  63 */     return false;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  68 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  69 */     Account acct = getRequestedAccount(zsc);
/*  70 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  71 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*  73 */     ItemId iid = new ItemId(request.getAttribute("id"), zsc);
/*  74 */     if (!iid.hasSubpart())
/*  75 */       throw ServiceException.INVALID_REQUEST("missing invId subpart: id should be specified as \"item-inv\"", null);
/*  76 */     int compNum = (int)request.getAttributeLong("comp");
/*     */     
/*  78 */     CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
/*  79 */     if (calItem == null)
/*  80 */       throw MailServiceException.NO_SUCH_CALITEM(iid.getId(), " for CancelCalendarItemRequest(" + iid + "," + compNum + ")");
/*  81 */     if (calItem.inTrash()) {
/*  82 */       throw ServiceException.INVALID_REQUEST("cannot cancel a calendar item under trash", null);
/*     */     }
/*     */     
/*     */ 
/*  86 */     Invite inv = calItem.getInvite(iid.getSubpartId(), compNum);
/*  87 */     if (inv == null) {
/*  88 */       throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
/*     */     }
/*  90 */     CalendarRequest.MailSendQueue sendQueue = new CalendarRequest.MailSendQueue();
/*     */     try {
/*  92 */       Element recurElt = request.getOptionalElement("inst");
/*  93 */       if (recurElt != null) {
/*  94 */         TimeZoneMap tzmap = inv.getTimeZoneMap();
/*  95 */         Element tzElem = request.getOptionalElement("tz");
/*  96 */         ICalTimeZone tz = null;
/*  97 */         if (tzElem != null) {
/*  98 */           tz = CalendarUtils.parseTzElement(tzElem);
/*  99 */           tzmap.add(tz);
/*     */         }
/* 101 */         RecurId recurId = CalendarUtils.parseRecurId(recurElt, tzmap);
/*     */         
/*     */ 
/* 104 */         ZimbraLog.calendar.info("<CancelCalendarItem> id=%d, folderId=%d, subject=\"%s\", UID=%s, recurId=%s", new Object[] { Integer.valueOf(calItem.getId()), Integer.valueOf(calItem.getFolderId()), inv.isPublic() ? inv.getName() : "(private)", calItem.getUid(), recurId.getDtZ() });
/*     */         
/*     */ 
/*     */ 
/* 108 */         Element msgElem = request.getOptionalElement("m");
/* 109 */         cancelInstance(zsc, octxt, msgElem, acct, mbox, calItem, inv, recurId, inv.getAttendees(), sendQueue);
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 114 */         ZimbraLog.calendar.info("<CancelCalendarItem> id=%d, folderId=%d, subject=\"%s\", UID=%s", new Object[] { Integer.valueOf(calItem.getId()), Integer.valueOf(calItem.getFolderId()), inv.isPublic() ? inv.getName() : "(private)", calItem.getUid() });
/*     */         
/*     */ 
/*     */ 
/* 118 */         Invite seriesInv = calItem.getDefaultInviteOrNull();
/* 119 */         if (seriesInv != null) {
/* 120 */           if ((seriesInv.getMethod().equals(ZCalendar.ICalTok.REQUEST.toString())) || (seriesInv.getMethod().equals(ZCalendar.ICalTok.PUBLISH.toString())))
/*     */           {
/*     */ 
/* 123 */             if (seriesInv.isOrganizer())
/*     */             {
/*     */ 
/*     */ 
/* 127 */               List<ZAttendee> atsSeries = seriesInv.getAttendees();
/* 128 */               Invite[] invs = calItem.getInvites();
/* 129 */               long now = octxt != null ? octxt.getTimestamp() : System.currentTimeMillis();
/* 130 */               for (Invite exceptInv : invs) {
/* 131 */                 if (exceptInv != seriesInv) {
/* 132 */                   String mthd = exceptInv.getMethod();
/* 133 */                   if (((mthd.equals(ZCalendar.ICalTok.REQUEST.toString())) || (mthd.equals(ZCalendar.ICalTok.PUBLISH.toString()))) && (inviteIsAfterTime(exceptInv, now)))
/*     */                   {
/* 135 */                     List<ZAttendee> atsExcept = exceptInv.getAttendees();
/*     */                     
/* 137 */                     List<ZAttendee> ats = CalendarUtils.getRemovedAttendees(atsExcept, atsSeries, false, acct);
/* 138 */                     if (!ats.isEmpty())
/*     */                     {
/* 140 */                       cancelInstance(zsc, octxt, null, acct, mbox, calItem, exceptInv, exceptInv.getRecurId(), ats, sendQueue);
/*     */                     }
/*     */                   }
/*     */                 }
/*     */               }
/*     */             }
/*     */             
/*     */ 
/* 148 */             Element msgElem = request.getOptionalElement("m");
/* 149 */             cancelInvite(zsc, octxt, msgElem, acct, mbox, calItem, seriesInv, sendQueue);
/*     */           }
/*     */           
/* 152 */           octxt = new OperationContext(octxt).unsetChangeConstraint();
/*     */         }
/*     */       }
/*     */     } finally {
/* 156 */       sendQueue.send();
/*     */     }
/*     */     
/* 159 */     Element response = getResponseElement(zsc);
/* 160 */     return response;
/*     */   }
/*     */   
/*     */   void cancelInstance(ZimbraSoapContext zsc, OperationContext octxt, Element msgElem, Account acct, Mailbox mbox, CalendarItem calItem, Invite inv, RecurId recurId, List<ZAttendee> toNotify, CalendarRequest.MailSendQueue sendQueue)
/*     */     throws ServiceException
/*     */   {
/* 166 */     boolean onBehalfOf = isOnBehalfOfRequest(zsc);
/* 167 */     Account authAcct = getAuthenticatedAccount(zsc);
/* 168 */     Locale locale = !onBehalfOf ? acct.getLocale() : authAcct.getLocale();
/* 169 */     String text = L10nUtil.getMessage(L10nUtil.MsgKey.calendarCancelAppointmentInstance, locale, new Object[0]);
/*     */     
/* 171 */     Invite cancelInvite = CalendarUtils.buildCancelInstanceCalendar(acct, authAcct, zsc.isUsingAdminPrivileges(), onBehalfOf, calItem, inv, text, recurId);
/* 172 */     CalendarRequest.CalSendData dat = new CalendarRequest.CalSendData();
/* 173 */     dat.mOrigId = new ItemId(mbox, inv.getMailItemId());
/* 174 */     dat.mReplyType = MailSender.MSGTYPE_REPLY;
/* 175 */     dat.mInvite = cancelInvite;
/*     */     
/* 177 */     ZCalendar.ZVCalendar iCal = dat.mInvite.newToICalendar(true);
/*     */     
/*     */ 
/* 180 */     if (msgElem != null) {
/* 181 */       String desc = ParseMimeMessage.getTextPlainContent(msgElem);
/* 182 */       String html = ParseMimeMessage.getTextHtmlContent(msgElem);
/* 183 */       iCal.addDescription(desc, html);
/*     */       
/* 185 */       MimeBodyPart[] mbps = new MimeBodyPart[1];
/* 186 */       mbps[0] = CalendarMailSender.makeICalIntoMimePart(iCal);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 191 */       dat.mMm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, mbox, msgElem, mbps, ParseMimeMessage.NO_INV_ALLOWED_PARSER, dat);
/*     */     }
/*     */     else
/*     */     {
/* 195 */       List<Address> rcpts = CalendarMailSender.toListFromAttendees(toNotify);
/* 196 */       dat.mMm = CalendarMailSender.createCancelMessage(acct, authAcct, zsc.isUsingAdminPrivileges(), onBehalfOf, rcpts, calItem, cancelInvite, text, iCal);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 201 */     doRecipientsCheck(dat, inv.isOrganizer(), mbox);
/* 202 */     sendCalendarCancelMessage(zsc, octxt, calItem.getFolderId(), acct, mbox, dat, true, sendQueue);
/*     */   }
/*     */   
/*     */   protected void cancelInvite(ZimbraSoapContext zsc, OperationContext octxt, Element msgElem, Account acct, Mailbox mbox, CalendarItem calItem, Invite inv, CalendarRequest.MailSendQueue sendQueue)
/*     */     throws ServiceException
/*     */   {
/* 208 */     boolean onBehalfOf = isOnBehalfOfRequest(zsc);
/* 209 */     Account authAcct = getAuthenticatedAccount(zsc);
/* 210 */     Locale locale = !onBehalfOf ? acct.getLocale() : authAcct.getLocale();
/* 211 */     String text = L10nUtil.getMessage(L10nUtil.MsgKey.calendarCancelAppointment, locale, new Object[0]);
/*     */     
/* 213 */     CalendarRequest.CalSendData csd = new CalendarRequest.CalSendData();
/* 214 */     csd.mOrigId = new ItemId(mbox, inv.getMailItemId());
/* 215 */     csd.mReplyType = MailSender.MSGTYPE_REPLY;
/* 216 */     csd.mInvite = CalendarUtils.buildCancelInviteCalendar(acct, authAcct, zsc.isUsingAdminPrivileges(), onBehalfOf, calItem, inv, text);
/*     */     
/* 218 */     ZCalendar.ZVCalendar iCal = csd.mInvite.newToICalendar(true);
/*     */     
/*     */ 
/* 221 */     if (msgElem != null) {
/* 222 */       String desc = ParseMimeMessage.getTextPlainContent(msgElem);
/* 223 */       String html = ParseMimeMessage.getTextHtmlContent(msgElem);
/* 224 */       iCal.addDescription(desc, html);
/*     */       
/* 226 */       MimeBodyPart[] mbps = new MimeBodyPart[1];
/* 227 */       mbps[0] = CalendarMailSender.makeICalIntoMimePart(iCal);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 232 */       csd.mMm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, mbox, msgElem, mbps, ParseMimeMessage.NO_INV_ALLOWED_PARSER, csd);
/*     */     } else {
/*     */       List<Address> rcpts;
/*     */       List<Address> rcpts;
/* 236 */       if (inv.isOrganizer()) {
/* 237 */         rcpts = CalendarMailSender.toListFromAttendees(inv.getAttendees());
/*     */       } else
/* 239 */         rcpts = new ArrayList(0);
/* 240 */       csd.mMm = CalendarMailSender.createCancelMessage(acct, authAcct, zsc.isUsingAdminPrivileges(), onBehalfOf, rcpts, calItem, inv, text, iCal);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 245 */     doRecipientsCheck(csd, inv.isOrganizer(), mbox);
/* 246 */     sendCalendarCancelMessage(zsc, octxt, calItem.getFolderId(), acct, mbox, csd, true, sendQueue);
/*     */   }
/*     */   
/*     */   private void doRecipientsCheck(CalendarRequest.CalSendData dat, boolean isOrganizer, Mailbox mbox) throws ServiceException {
/* 250 */     boolean hasRecipients = false;
/*     */     try {
/* 252 */       Address[] rcpts = dat.mMm.getAllRecipients();
/* 253 */       hasRecipients = (rcpts != null) && (rcpts.length > 0);
/*     */     } catch (MessagingException e) {
/* 255 */       throw ServiceException.FAILURE("Checking recipients of outgoing msg ", e);
/*     */     }
/* 257 */     if (hasRecipients) {
/* 258 */       if (isOrganizer)
/*     */       {
/*     */ 
/*     */ 
/* 262 */         mbox.getMailSender().checkMTAConnection();
/*     */       }
/*     */       else {
/* 265 */         throw MailServiceException.MUST_BE_ORGANIZER("CancelCalendarItem");
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CancelCalendarItem.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */